This article mainly introduces the Java method to keep the double type two decimal places, please refer to the use of itCopy CodeThe code is as follows:Mport Java.text.DecimalFormat;DecimalFormat df = new DecimalFormat ("##### #0.00");Double D1 = 3.2
Java: Rounding the double value to five, retaining the two decimal places, double rounding to five1. Functions
The double value in the program is accurate to the two digits after the decimal point. It can be rounded up or truncated.
For example, if
1. functionThe double value in the program is accurate to two digits after the decimal point. Can be rounded or truncated directly.For example: Enter 12345.6789, the output can be 12345.68 or it can be 12345.67. As to whether rounding is required,
using Java to realize the reciprocal mechanism between decimal and binary decimal
This blog mainly describes the use of Java to achieve decimal and binary decimal number of the mutual transfer (do not invoke Java-brought functions). Later may use
Packagecom.clzhang.sample;ImportJava.math.BigDecimal;ImportJava.math.RoundingMode;ImportJava.text.DecimalFormat;ImportJava.text.NumberFormat; Public classDoubletest {/*** Keep Two decimal places, rounding up an old-fashioned way *@paramd *@return
Mode one:RoundedDouble F = 111231.5585;Rounding to keep two decimal digits, you can use the Format function of string,The method is as follows:
Copy Code code as follows:
System.out.println (String.Format ("%.2f", X1));
Reprint:http://blog.csdn.net/wj_j2ee/article/details/8560132Java retains two-bit decimal questions:Way One:RoundedDouble F = 111231.5585;BigDecimal B = new BigDecimal (f);Double f1 = B.setscale (2, bigdecimal.round_half_up). Doublevalue ();Keep two
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.